/**
 * hsv 转 rgb
 * @param {Number} h Hue
 * @param {Number} s Saturation
 * @param {Number} v Value
 * @returns {String} rgb
 */
export function hsv2rgb(h, s, v) {
    let hsv_h = (h / 360).toFixed(2);
    let hsv_s = (s / 100).toFixed(2);
    let hsv_v = (v / 100).toFixed(2);

    var i = Math.floor(hsv_h * 6);
    var f = hsv_h * 6 - i;
    var p = hsv_v * (1 - hsv_s);
    var q = hsv_v * (1 - f * hsv_s);
    var t = hsv_v * (1 - (1 - f) * hsv_s);

    var rgb_r = 0,
        rgb_g = 0,
        rgb_b = 0;
    switch (i % 6) {
        case 0:
            rgb_r = hsv_v;
            rgb_g = t;
            rgb_b = p;
            break;
        case 1:
            rgb_r = q;
            rgb_g = hsv_v;
            rgb_b = p;
            break;
        case 2:
            rgb_r = p;
            rgb_g = hsv_v;
            rgb_b = t;
            break;
        case 3:
            rgb_r = p;
            rgb_g = q;
            rgb_b = hsv_v;
            break;
        case 4:
            rgb_r = t;
            rgb_g = p;
            rgb_b = hsv_v;
            break;
        case 5:
            rgb_r = hsv_v, rgb_g = p, rgb_b = q;
            break;
    }

    return 'rgb(' + (Math.floor(rgb_r * 255) + "," + Math.floor(rgb_g * 255) + "," + Math.floor(rgb_b * 255)) + ')';
};
/**
 * rgb 转 hsv
 * @param {String} color rgb
 * @returns {Object} h s v
 */
export function rgb2hsv(color) {
    let rgb = color.split(',');
    let R = parseInt(rgb[0].split('(')[1]);
    let G = parseInt(rgb[1]);
    let B = parseInt(rgb[2].split(')')[0]);

    let hsv_red = R / 255, hsv_green = G / 255, hsv_blue = B / 255;
    let hsv_max = Math.max(hsv_red, hsv_green, hsv_blue),
        hsv_min = Math.min(hsv_red, hsv_green, hsv_blue);
    let hsv_h, hsv_s, hsv_v = hsv_max;

    let hsv_d = hsv_max - hsv_min;
    hsv_s = hsv_max == 0 ? 0 : hsv_d / hsv_max;

    if (hsv_max == hsv_min) hsv_h = 0;
    else {
        switch (hsv_max) {
            case hsv_red:
                hsv_h = (hsv_green - hsv_blue) / hsv_d + (hsv_green < hsv_blue ? 6 : 0);
                break;
            case hsv_green:
                hsv_h = (hsv_blue - hsv_red) / hsv_d + 2;
                break;
            case hsv_blue:
                hsv_h = (hsv_red - hsv_green) / hsv_d + 4;
                break;
        }
        hsv_h /= 6;
    }
    return {
        h: (hsv_h * 360).toFixed(),
        s: (hsv_s * 100).toFixed(),
        v: (hsv_v * 100).toFixed()
    }
};
/**
 * rgb 转 hex
 * @param {string} color rgb(0, 0, 0)
 * @returns {String} hex:'000000'
 */
export function rgb2hex(color) {
    let rgb = color.split(',');
    let R = parseInt(rgb[0].split('(')[1]);
    let G = parseInt(rgb[1]);
    let B = parseInt(rgb[2].split(')')[0]);
    let hex = '#' + ((1 << 24) + (R << 16) + (G << 8) + B).toString(16).slice(1);
    return hex.toUpperCase();
};
/**
 * hex 转 rgb
 * @param {String} hex:'#888888' 
 * @return {String} 'rgb(255,0,0)'
 */
export function hex2rgb(hex) {
    if (hex.length === 4) {
        let hexNew = "#";
        for (var i = 1; i < 4; i += 1) {
            hexNew += hex.slice(i, i + 1).concat(hex.slice(i, i + 1));
        }
        hex = hexNew;
    }
    return "rgb(" + parseInt("0x" + hex.slice(1, 3)) + "," + parseInt("0x" + hex.slice(3, 5)) + "," + parseInt("0x" + hex.slice(5, 7)) + ")";
};